package main

import (
	"errors"
	"fmt"
)

type stackQueue struct {
	lStack *stack
	rStack *stack
}

type stack struct {
	head *listNode
}

type listNode struct {
	val int
	next *listNode
}

func newStack()*stack{
	return &stack{
	}
}

func (s *stack)isEmpty()bool{
	return s.head == nil
}

func (s *stack)Push(val int) {
	s.head = &listNode{
		next:s.head,
		val:val,
	}
}

func (s *stack)Pop()int{
	if s.head == nil {
		panic(errors.New("size is empty"))
	}
	res := s.head.val
	s.head = s.head.next
	return res
}

func newStackQue()*stackQueue{
	return &stackQueue{
		lStack: newStack(),
		rStack: newStack(),
	}
}

func (sq *stackQueue)Push(val int){
	sq.lStack.Push(val)
}

func (sq *stackQueue)Pop()int{
	var res int
	if sq.rStack.isEmpty() {
		if sq.lStack.isEmpty(){
			panic(errors.New("size is 0"))
		}else{
			for !sq.lStack.isEmpty() {
				ele := sq.lStack.Pop()
				sq.rStack.Push(ele)
			}
			res = sq.rStack.Pop()
		}
	} else {
		res = sq.rStack.Pop()
	}
	return res
}

func main(){
	rStack:= newStack()
	fmt.Println(rStack.isEmpty())
	queue := newStackQue()
	queue.Push(1)
	queue.Push(2)
	queue.Push(3)
	queue.Push(4)
	queue.Push(5)
	fmt.Println(queue.Pop())
	fmt.Println(queue.Pop())
	fmt.Println(queue.Pop())
	queue.Push(8)
	fmt.Println(queue.Pop())
}